<?php

class EstadisticaGeneral
{
    private $estadisticas,$porUsuario, $porDisciplina;
    function __construct()
    {
        $db = new BaseDatos(DBHOST, DBUSER, DBPASS, DBBASE);
        $datos = $db->selectAsociativo("SELECT t2.usr_id AS id_usuario, t2.dis_id AS id_disciplina, sum( t2.cant ) AS cantidad, dis_minimo AS minimo, dis_maximo AS maximo
            FROM (
                    SELECT usr_id, dis_id, IF( ISNULL( inscriptos.id ) , 0, 1 ) AS cant, dis_minimo, dis_maximo
                    FROM (
                            SELECT usuarios.id AS usr_id, disciplinas.id AS dis_id, disciplinas.minimo AS dis_minimo, disciplinas.maximo AS dis_maximo
                            FROM usuarios, disciplinas
                            ORDER BY usuarios.id, disciplinas.id
                        ) AS T1
                    LEFT JOIN inscriptos ON (inscriptos.id_usuario = T1.usr_id AND inscriptos.id_disciplina  =  T1.dis_id )
                 ) AS t2
            GROUP BY t2.usr_id, t2.dis_id");
        $dscIds = $db->selectAsociativo("SELECT id FROM disciplinas");
        foreach($dscIds as $i)
            $this->porDisciplina[$i['id']]=0;
        $usrIds = $db->selectAsociativo("SELECT id FROM usuarios");
        foreach($usrIds as $i)
            $this->porUsuario[$i['id']]=0;
        unset($db);
        foreach($datos as $d)
        {
            if ($d['cantidad'] == 0)
                $e = EstadoInscripcionVacio::getInstancia();
            else if ($d['cantidad'] < $d['minimo'])
                $e = EstadoInscripcionInsuficiente::getInstancia();
            else if ($d['cantidad'] < $d['maximo'])
                $e = EstadoInscripcionOk::getInstancia();
            else if ($d['cantidad'] == $d['maximo'])
                $e = EstadoInscripcionOptimo::getInstancia();
            else
                die;
            $this->estadisticas[$d['id_usuario']][$d['id_disciplina']] = new EstadisticaSimple($d['cantidad'], $e);
            
            if ($d['cantidad'] >= $d['minimo'])
            {
                $this->porDisciplina[$d['id_disciplina']]++;
                $this->porUsuario[$d['id_usuario']]++;
            }
        }
    }
    
    public function getEstadistica($idUsuario, $idDisciplina)
    {
        return $this->estadisticas[$idUsuario][$idDisciplina];
    }
    
    public function getEstadisticaPorUsuario($idUsuario)
    {
        
        return $this->porUsuario[$idUsuario];
    }
    
    public function getEstadisticaPorDisciplina($idDisciplina)
    {
        
        return $this->porDisciplina[$idDisciplina];
    }
    



}

class EstadisticaSimple
{
    private $cantidad, $estado;
    function __construct($cantidad, $estado) {
        $this->cantidad = $cantidad;
        $this->estado = $estado;
    }
    public function getCantidad() {
        return $this->cantidad;
    }

    public function getEstado() {
        return $this->estado;
    }



            
}

?>
